package com.example.springLearning.part11.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/spring/part11")
public class Spring11Controller {

    @RequestMapping(path = "/hi",method = RequestMethod.GET)
    public String hi(@RequestHeader("myHeaderName") String name) {
        return name;
    }

    /**
     * 存在问题
     * @param map
     * @return
     */
    @RequestMapping(path = "/hi1",method = RequestMethod.GET)
    public String hi1(@RequestHeader() Map map) {
        log.info(map.toString());
        return "ok";
    }

    /**
     * 解决办法一
     */
    @RequestMapping(path = "/hi2",method = RequestMethod.GET)
    public String hi2(@RequestHeader() MultiValueMap map) {
        log.info(map.toString());
        return "ok";
    }

    /**
     * 解决办法二
     */
    @RequestMapping(path = "/hi3",method = RequestMethod.GET)
    public String hi3(@RequestHeader() HttpHeaders map) {
        log.info(map.toString());
        return "ok";
    }

    @RequestMapping(path = "/hi4", method = RequestMethod.GET)
    public String hi4(@RequestHeader("MyHeader") String myHeader, @RequestHeader MultiValueMap map ) {
        return myHeader + "compare with : " + map.get("MyHeader");
    }

    @RequestMapping(path = "/hi5", method = RequestMethod.GET)
    public String hi5(@RequestHeader("MyHeader") String myHeader, @RequestHeader MultiValueMap map ) {
        return myHeader + "compare with : " + map.get("myheader");
    }

    @RequestMapping(path = "/hi6", method = RequestMethod.GET, produces = {"application/json"})
    public String hi6(HttpServletResponse response) {
        response.addHeader("myheader", "myheadervalue");
        response.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        return "ok";
    }
}
